package com.ck.easy.controller;

import com.ck.common.BaseTask;
import com.ck.easy.common.entity.SourceEntity;
import com.ck.easy.excel.EasyExcelUtil;
import com.ck.easy.listener.DefaultExcelListener;
import com.ck.easy.service.EasyExcelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ck
 * @date 2023/8/25 15:56
 * desc: db与excel文件之间的交互
 */
@RestController
@RequestMapping("/easy")
@Slf4j
public class EasyExcelController extends BaseTask {

    @Autowired
    EasyExcelService easyExcelService;


    /**
     * 文件导出(db->excel)
     * @param response
     * @throws IOException
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
      log.info("开始导出文件");
      long start = taskStart();
      easyExcelService.exportFile(response);
      taskEnd(start);
      log.info("导出文件结束");
    }

    /**
     * 文件导入(excel->db)
     * @throws IOException
     */
    @PostMapping("/import")
    public void export2() throws IOException {
        log.info("开始导入文件");
        long start = taskStart();
        String fileName="D:\\changkang-youth\\export\\1714267292292_af6325f3-7c63-467a-b7ae-3c53e3de671b学生成绩表.xlsx";
        EasyExcelUtil.asyncReadModel(fileName, new DefaultExcelListener<>(easyExcelService), SourceEntity.class,0,1);
        taskEnd(start);
        log.info("导入文件结束");
    }



}
